package com.rabbitmq;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class DirectServer {
   public static void main(String[] args) throws IOException, TimeoutException {
	
	    ConnectionFactory connectionFactory=new ConnectionFactory();
	    connectionFactory.setVirtualHost("/");
	    connectionFactory.setPort(5672);
	    connectionFactory.setUsername("zhangsan");
	    connectionFactory.setPassword("zhangsan");
	    connectionFactory.setHost("112.74.214.177");
	    connectionFactory.setConnectionTimeout(6000);
	    connectionFactory.setRequestedHeartbeat(100);
	    
	    Connection connection=connectionFactory.newConnection();

	    
	    Channel channel=connection.createChannel();
	    
	    channel.exchangeDeclare("Exchange00000",BuiltinExchangeType.DIRECT,true);
	    
	    channel.exchangeDeclare("DEAD-Exchange00000",BuiltinExchangeType.DIRECT,true);//生命死新队列的交换器
	    //queue Name  是否持久化  exculsize  auto_delete  队列设置
	    Map<String,Object> map=new HashMap<String,Object>();
	    //map.put("x-message-ttl", 5000);//设置超时
	    map.put("x-dead-letter-exchange","DEAD-Exchange00000");//绑定私信交换器
	    map.put("x-dead-letter-routing-key","DEAD-KEY");//绑定死信路由键
	    
	    channel.queueDeclare("Queue00000", true,false,false,map);
	    
	    channel.queueDeclare("DEAD-Queue00000", true,false,false,map);//死信队列
	    
	    
	    //1队列名称  2交换器名称  3路由键
	    channel.queueBind("Queue00000","Exchange00000","directMessage");
	    
	    channel.queueBind("DEAD-Queue00000","DEAD-Exchange00000","DEAD-KEY");
	   
	    
	    //1交换器名称  2路由键  
	    BasicProperties.Builder build=new BasicProperties().builder();
	    build.expiration("10000");
	    BasicProperties basicProperties=build.build();
	    channel.basicPublish("Exchange00000","directMessage",basicProperties,"你好，渣渣！".getBytes());
	    //channel.basicPublish("Exchange00000","directMessage",null,"你好，渣渣！".getBytes());

	    
	
	    
	    channel.close();
	    
	    connection.close();
	    
   }
}
